package com.logica.mdm.webservices.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.logica.mdm.webservices.dao.SearchXrefDao;
import com.logica.mdm.webservices.dao.beans.XrefResultBean;

/**
 * Implementation using NamedParameterJdbcTemplate
 */
@Repository
public class JdbcSearchXrefDaoImpl implements SearchXrefDao {

	private NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
	    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<XrefResultBean> searchXref(String sqlQuery, Map<String, ?> sqlParams) {
		return this.jdbcTemplate.query(sqlQuery, sqlParams, new XrefMapper());
	}
	
	/**
	 * Inner class defining the mapping between the database and PartyByPhoneNumberResultBean
	 */
	private static final class XrefMapper implements RowMapper<XrefResultBean> {
		public XrefResultBean mapRow(ResultSet rs, int rowNum) throws SQLException {
			XrefResultBean xref = new XrefResultBean();
			xref.setRowidObject(rs.getString("ROWID_OBJECT"));
			xref.setPkeySrcObject(rs.getString("PKEY_SRC_OBJECT"));
	        return xref;
	    }
	}

}
